package com.hibase.core.rule.service.rulemenu.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import com.hibase.core.rule.constant.RuleStatus;
import com.hibase.core.rule.constant.RuleType;
import com.hibase.core.rule.entity.rulemenu.RuleMenu;
import com.hibase.core.rule.entity.rulemenu.RuleMenuContent;
import com.hibase.core.rule.entity.rulemenu.vo.RuleMenuContentSearchVO;
import com.hibase.core.rule.mapper.rulemenu.RuleMenuContentMapper;
import com.hibase.core.rule.service.rulemenu.RuleMenuContentService;
import com.hibase.core.rule.service.rulemenu.RuleMenuService;
import com.hibase.core.rule.utils.HibaseRuleEngineUtils;
import com.hibase.core.rule.utils.RedisUtils;
import com.hibase.common.exception.HibaseAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 * 规则菜单内容表 服务实现类
 * </p>
 *
 * @author hufeng
 * @since 2019-06-10
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class RuleMenuContentServiceImpl extends BaseServiceImpl<RuleMenuContentMapper, RuleMenuContent> implements RuleMenuContentService {

    @Autowired
    private RuleMenuService ruleMenuService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private HibaseRuleEngineUtils hibaseRuleEngineUtils;

    /**
     * 查询规则菜单内容表列表
     */
    @Override
    public PageInfo<RuleMenuContent> selectRuleMenuContentList(RuleMenuContentSearchVO searchVO) {

        return super.page(searchVO, new QueryWrapper<RuleMenuContent>());
    }

    /**
     * 改变规则菜单内容表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRuleMenuContent(RuleMenuContent entity) {

        RuleMenuContent name = super.getOne(new QueryWrapper<RuleMenuContent>().eq("name", entity.getName()));
        if (StrUtil.isNotBlank(entity.getId())) {
            // 修改
            HibaseAssert.isTrue(name==null||name.getId().equals(entity.getId()),"名称已存在");

        }else {
            // 新增
            HibaseAssert.isNull(name,"名称已存在");

            // 更新所属菜单目录节点数
            RuleMenu ruleMenu = this.ruleMenuService.getById(entity.getMenuId());

            ruleMenu.setChildQty(ruleMenu.getChildQty().add(BigDecimal.ONE));

            this.ruleMenuService.store(ruleMenu);
        }

        super.store(entity);

        redisUtils.set(entity.getId(), entity);

    }

    /**
     * 查询规则菜单内容表详情
     */
    @Override
    public RuleMenuContent viewRuleMenuContent(String id) {

        RuleMenuContent entity = (RuleMenuContent) redisUtils.get(id);

        if (entity == null) {

            entity = super.getById(id, true);

            redisUtils.set(id, entity);

        }

        return entity;
    }

    /**
     * 删除规则菜单内容表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRuleMenuContent(String id) {

        RuleMenuContent old = super.getById(id);

        super.deleteBy(id);

        redisUtils.set(id,null);

        // 更新所属菜单目录节点数
        RuleMenu ruleMenu = this.ruleMenuService.getById(old.getMenuId());

        ruleMenu.setChildQty(ruleMenu.getChildQty().subtract(BigDecimal.ONE));

        this.ruleMenuService.store(ruleMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void operaRuleMenuContent(String id, String operaType) {
        //上线检查语法


        RuleMenuContent content = (RuleMenuContent) redisUtils.get(id);

        if (content == null) {

            content = super.getById(id, true);

            redisUtils.set(id, content);

        }
        if(RuleStatus.RULE_STATUS_RELEASE.getKey().equals(operaType)&&content.getType().equals(RuleType.RULE_TYPE_RULE.getName())){
            if(StrUtil.isNotBlank(content.getContent())){
                HibaseAssert.isTrue(hibaseRuleEngineUtils.checkSyntax(content.getContent()),"语法不正确,请检查语法!");
            }
        }
        boolean checkResult = operaType.equals(content.getStatus());

        HibaseAssert.isFalse(checkResult, "请勿重复操作");

        content.setStatus(operaType);

        super.store(content);

        redisUtils.set(content.getId(), content);
    }
}
